package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.model.entity.Goods;
import com.community.model.entity.GoodsStore;
import com.community.model.entity.Store;
import com.community.web.master.domain.dto.PageStoreDto;
import com.community.web.master.domain.dto.PageStoreGoodsDto;
import com.community.web.master.domain.dto.StoreDto;
import com.community.web.master.domain.dto.StoreGoodsDto;
import com.community.web.master.domain.vo.*;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.GoodsMapper;
import com.community.web.master.mapper.GoodsStoreMapper;
import com.community.web.master.mapper.StoreMapper;
import com.community.web.master.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private GoodsStoreMapper goodsStoreMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 门店分页查询
     * @param pageStoreDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageStoreDto pageStoreDto) {
        QueryWrapper<Store> queryWrapper= new QueryWrapper<Store>();
        if(pageStoreDto.getStoreName()!=null){
            if(!pageStoreDto.getStoreName().equals("")){
                queryWrapper.like("store_name",pageStoreDto.getStoreName());
            }
        }
        if(pageStoreDto.getAreaId()!=null){
            queryWrapper.eq("area_id",pageStoreDto.getAreaId());
        }
        Long total = storeMapper.selectCount(queryWrapper);

        Map<String,Object> map=new HashMap<>();
        map.put("limit",(pageStoreDto.getPageSize()));
        map.put("offset",(pageStoreDto.getPage()-1)*pageStoreDto.getPageSize());
        map.put("store_name",pageStoreDto.getStoreName());
        map.put("area_id",pageStoreDto.getAreaId());

        List<StoreWithAreaVo> result= storeMapper.selectPageByMap(map);
        return new PageResult(total,result);
    }

    /**
     * 根据门店编号分页查询商品
     * @param pageStoreGoodsDto
     * @return
     */
    @Override
    public PageResult pageQueryGoods(PageStoreGoodsDto pageStoreGoodsDto) {
        //查出商品号
        List<GoodsStore> goodsStores = goodsStoreMapper.selectList(new QueryWrapper<GoodsStore>().eq("store_no", pageStoreGoodsDto.getStoreNo()));
        List<String> goodsNos = goodsStores.stream().map(GoodsStore::getGoodsNo).collect(Collectors.toList());
        //查出total
        QueryWrapper<Goods> queryWrapper= new QueryWrapper<Goods>();
        if(pageStoreGoodsDto.getGoodsName()!=null){
            if(!pageStoreGoodsDto.getGoodsName().equals("")){
                queryWrapper.like("goods_name",pageStoreGoodsDto.getGoodsName());
            }
        }
        if(pageStoreGoodsDto.getCategoryId()!=null){
            if(pageStoreGoodsDto.getCategoryId()==0) throw new DataException("信息有误");
            queryWrapper.eq("category_id",pageStoreGoodsDto.getCategoryId());
        }
        queryWrapper.in("goods_no",goodsNos);
        Long total = goodsMapper.selectCount(queryWrapper);

        //查询list
        Map<String,Object> map = new HashMap<>();
        map.put("goods_name",pageStoreGoodsDto.getGoodsName());
        map.put("store_no",pageStoreGoodsDto.getStoreNo());
        map.put("category_id",pageStoreGoodsDto.getCategoryId());
        map.put("goods_nos",goodsNos);
        map.put("limit",(pageStoreGoodsDto.getPageSize()));
        map.put("offset",(pageStoreGoodsDto.getPage()-1)*pageStoreGoodsDto.getPageSize());
        List<StoreGoodsVo> goods = storeMapper.selectGoodsPageByMap(map);

        return new PageResult(total,goods);
    }

    /**
     * 添加商品
     * @param storeGoodsDto
     */
    @Override
    public void addGoods(StoreGoodsDto storeGoodsDto) {
        GoodsStore goodsStore = new GoodsStore();
        if( null ==goodsMapper.selectOne(new QueryWrapper<Goods>().eq("goods_no",storeGoodsDto.getGoodsNo()))) throw new DataException("商品不存在");
        if(goodsStoreMapper.selectOne(new QueryWrapper<GoodsStore>()
                .eq("goods_no", storeGoodsDto.getGoodsNo()).eq("store_no", storeGoodsDto.getStoreNo()))!=null)
            throw new DataException("商品已经存在");
        BeanUtils.copyProperties(storeGoodsDto,goodsStore);
        goodsStoreMapper.insert(goodsStore);
    }

    /**
     * 更新商品
     * @param storeGoodsDto
     */

    @Override
    public void updateGoods(StoreGoodsDto storeGoodsDto) {
        if( null ==goodsMapper.selectOne(new QueryWrapper<Goods>().eq("goods_no",storeGoodsDto.getGoodsNo()))) throw new DataException("商品不存在");
        GoodsStore goodsStore = goodsStoreMapper.selectOne(new QueryWrapper<GoodsStore>().eq("goods_no", storeGoodsDto.getGoodsNo()).eq("store_no", storeGoodsDto.getStoreNo()));
        if(goodsStore==null) throw new DataException("商品不存在");
        BeanUtils.copyProperties(storeGoodsDto,goodsStore);
        goodsStoreMapper.updateById(goodsStore);
    }

    /**
     * 删除商品
     * @param storeGoodsDto
     */
    @Override
    public void deleteGoods(StoreGoodsDto storeGoodsDto) {
        if( null ==goodsMapper.selectOne(new QueryWrapper<Goods>().eq("goods_no",storeGoodsDto.getGoodsNo()))) throw new DataException("商品不存在");
        QueryWrapper<GoodsStore> queryWrapper=new QueryWrapper<GoodsStore>();
        queryWrapper.eq("goods_no",storeGoodsDto.getGoodsNo());
        queryWrapper.eq("store_no",storeGoodsDto.getStoreNo());
        goodsStoreMapper.delete(queryWrapper);
    }

    /**
     * 新增门店
     * @param storeDto
     */
    @Override
    public void addStore(StoreDto storeDto) {
        if(storeDto.getStoreName()==null ||storeDto.getAreaId() ==null || storeDto.getStoreName().equals("")) throw new DataException("数据有误");
        if(storeMapper.selectOne(new QueryWrapper<Store>()
                .eq("store_name",storeDto.getStoreName())
                .eq("area_id",storeDto.getAreaId()))!=null)
            throw new DataException("门店已存在");
        Store store = new Store();
        //复制属性
        BeanUtils.copyProperties(storeDto,store);
        store.setId(null);
        store.setCreateTime(null);
        store.setUpdateTime(null);
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString().replace("-", "");
        store.setStoreNo(randomUUIDString);
        storeMapper.insert(store);
    }

    /**
     * 修改门店
     * @param storeDto
     */
    @Override
    public void updateStore(StoreDto storeDto) {
        if(storeDto.getStoreName()==null ||storeDto.getAreaId() ==null || storeDto.getStoreName().equals("")) throw new DataException("数据有误");
        Store oldStore = storeMapper.selectById(storeDto.getId());
        if(oldStore==null) throw new DataException("门店不存在");
        if(!oldStore.getStoreName().equals(storeDto.getStoreName())){
            if(storeMapper.selectOne(new QueryWrapper<Store>()
                    .eq("store_name",storeDto.getStoreName())
                    .eq("area_id",storeDto.getAreaId()))!=null)
                throw new DataException("门店已存在");
        }
        //修改
        BeanUtils.copyProperties(storeDto,oldStore);
        storeMapper.updateById(oldStore);

    }

    /**
     * 删除门店,并删除关联信息
     * @param id
     */
    @Override
    @Transactional
    public void deleteStore(Integer id) {
        Store store = storeMapper.selectById(id);
        goodsStoreMapper.delete(new QueryWrapper<GoodsStore>()
                .eq("store_no",store.getStoreNo()));
        storeMapper.deleteById(id);

    }
}
